package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import com.pojo.Dept;
import com.service.DeptService;

import java.util.List;

@RestController
public class DeptController {

    @Autowired
    DeptService deptService;
    @Autowired
    private DiscoveryClient client;

    @GetMapping("/dept")
    public List<Dept> listDept(){
        return  deptService.getDepartments();
    }

    @DeleteMapping("/dept/{id}")
    public void deleteDept(@PathVariable  int id){
       deptService.deleteDept(id);
    }

    @PostMapping("/dept")
    public void addDept(Dept dept){
        deptService.addDept(dept);
    }

    @GetMapping("/dept/discovery")
    public Object discovery() {
        // 获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:" + services);
        // 得到一个具体的微服务信息,通过具体的微服务id，applicaioinName；
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-7001");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t" + // 主机名称
                            instance.getPort() + "\t" + // 端口号
                            instance.getUri() + "\t" + // uri
                            instance.getServiceId() // 服务id
            );
        }
        return this.client;
    }
}
